import math


def get_communication_delay_v2(distance_m):
    uplink = 10.0  # Mhz 上行链路带宽
    pn = 0.5  # 1w
    no = 2e-3
    task_data_size = 10 * 1024 * 1024 * 8

    b = uplink * 1000 * 1000  # the channel bandwidth of the physical link
    tao = pn  # transmission power of the local mobile device
    channel_gain = 127 + 30 * math.log(distance_m)  # channel gain
    n = no  # noisy power
    tran = b * math.log2(1 + (tao * channel_gain) / n)

    return task_data_size / tran


def get_communication_delay(distance):
    uplink = 10.0  # Mhz 上行链路带宽
    pn = 1  # 1w
    no = 1e-14  # 14 15 16
    task_data_size = 10 * 1024 * 1024 * 8

    tmp_rn = uplink * 1000 * 1000
    z_distance = 5.0
    distance_2 = (math.pow(distance, 2) + math.pow(z_distance, 2))
    # todo 增加h 避免0的情况
    t = 1e-6
    h = t / distance_2  # 无线增益
    # # h = t * distance
    # h = pow(math.sqrt(distance_2), -3)
    sin = pn * h / no

    # 数据传输率
    data_trans_rate = tmp_rn * math.log2(
        1 + sin)  # 计算速率 (1 + transmission power *无线信道增益 /  信噪比)
    communication_delay = task_data_size / data_trans_rate

    # communication_delay = (task.data_size_mb)  / ((server.uplink) *math.log2(1+user.pn*h/server.no))
    return communication_delay


if __name__ == "__main__":
    print(get_communication_delay_v2(1000))
    print(get_communication_delay_v2(3000))

    print(get_communication_delay(100))
    print(get_communication_delay(1000))
    print(get_communication_delay(3000))
